﻿/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：曾永佑
 * 版本：V1.0
 * 创建日期：2013-03-10
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Configuration;
using System.Web.Configuration;
using SRF.Security.Permission;
using SRF.Security.Principal;

namespace SRF.Security
{
    /// <summary>
    /// 安全认证上下文
    /// </summary>
    public class SecurityContext
    {
        #region 属性

        /// <summary>
        /// 当前用户
        /// </summary>
        public static IUser User
        {
            get { return SecurityProvider.GetCurrentUser(); }
        }
        /// <summary>
        /// 安全认证提供程序
        /// </summary>
        public static ISecurityProvider SecurityProvider
        {
            get
            {
                SecurityProvider provider = null;
                SecuritySection section = (SecuritySection)ConfigurationManager.GetSection("SecurityProvider");
                if (section != null)
                {
                    SecurityProviderCollection _providers = new SecurityProviderCollection();
                    ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(SecurityProvider));
                    if (!string.IsNullOrEmpty(section.DefaultProvider))
                    {
                        provider = _providers[section.DefaultProvider];
                    }
                }
                if (provider == null)
                    provider = new SecurityProvider();

                return provider;
            }
        }
        /// <summary>
        /// 当前是否处于登录状态 
        /// </summary>
        public static bool IsLogin
        {
            get { return SecurityProvider.IsLogin; }
        }
         
        #endregion

        #region 

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginId">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        public static void Login(string loginId = "", string password = "", string code = "")
        {
            if (!SecurityProvider.IsLogin)
            {
                SecurityProvider.Login(loginId, password, code);

                if (LoginCompleted != null)
                    LoginCompleted();
            }
        } 
        /// <summary>
        /// 注销
        /// </summary>
        public static void Logout()
        {
            if (SecurityProvider.IsLogin)
            {
                SecurityProvider.Logout();

                if (LogoutCompleted != null)
                    LogoutCompleted();
            }
        }
        /// <summary>
        /// 当前是否有某项操作权限
        /// </summary>
        /// <param name="action">操作编码</param>
        /// <returns></returns>
        public static bool HasActionGrant(string action)
        {
            return SecurityProvider.HasActionGrant(action);
        }
        /// <summary>
        /// 当前是否有访问某个url的权限
        /// </summary>
        /// <param name="url">要检测的url</param>
        /// <returns></returns>
        public static bool HasUrlGrant(string url = "")
        {
            return SecurityProvider.HasUrlGrant(url);
        }
        /// <summary>
        /// 获取数据规则过滤条件
        /// </summary>
        /// <param name="code">数据规则编码</param>
        /// <returns></returns>
        public static string GetDataRuleCondition(string code)
        {
            return SecurityProvider.GetDataRuleCondition(code);
        }

        #endregion

        #region 事件

        /// <summary>
        /// 在登录成功时触发的事件
        /// </summary>
        public static event Action LoginCompleted;
        /// <summary>
        ///  在登出成功时触发的事件
        /// </summary>
        public static event Action LogoutCompleted; 
        
        #endregion
    }
}